const db = require('../db/index.js');

exports.getArtcate = function (req, res) {
    let sql = "SELECT * FROM ev_article_cate where is_delete=0 order by id asc";
    db.query(sql, (err, result) => {
        if (err) return res.cc(err);
        res.send(result)
    })
}

exports.addArtcate = function (req, res) {
    let { name, alias } = req.body;

    const sqlStr = "SELECT * FROM ev_article_cate WHERE name=? OR alias=?";
    db.query(sqlStr, [name, alias], (err, result) => {
        if (err) return res.cc(err);
        if (result.length > 0) return res.cc('分类名称或别名已存在');
        const sql = "insert into ev_article_cate (name,alias) values (?,?)";
        db.query(sql, [name, alias], (err, result) => {
            if (err) return res.cc(err);
            if(result.affectedRows==0)return res.cc('添加失败')
            res.send("添加成功")
        })
    })
}

exports.delArtcate = function (req, res) {
    console.log(req.params)

    const sql = "update ev_article_cate set is_delete=1 where id=?"
    db.query(sql, [req.params.id], (err, result) => {
        if (err) return res.cc(err);
        if (result.affectedRows == 0) return res.cc('删除失败');
        res.send("删除成功")
    })
}

